//
//  SGBaseHttpTool.m
//  MomHelp
//
//  Created by xuguoyong on 16/8/12.
//  Copyright © 2016年 hudongkeji. All rights reserved.
//

#import "SGBaseHttpTool.h"
#import "SGHttpTool.h"
#import <MJExtension.h>
@implementation SGBaseHttpTool
/**
 *   GET请求
 *
 *  @param url          请求地址
 *  @param param       请求参数模型
 *  @param resultClass 请求结果的模型
 *  @param show        请求结果的模型
 *  @param view        将蒙层展示在View上（如果View为空，则不显示蒙层）
 *  @param success     成功的回调
 *  @param failure     失败的回调
 */
+ (void)getWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass showResultMessage:(BOOL)show showHUD:(BOOL)hud success:(void (^)(id resultModel ,id responseObj))success failure:(void (^)(NSError *error,NSString *errorCode,NSString *remark))failure
{
    // 没有网络就不去请求数据 做一个友好的提示
    if (![SGShowMesssageTool isHaveNetWork]) {
        if (failure) {
            failure(nil,@"-10086",SGNotNetWorkingTips);
        }
        [SGShowMesssageTool showMessage:SGNotNetWorkingTips];
        
        return;
    }
    
    // 显示状态栏上面的菊花
    dispatch_async(dispatch_get_main_queue(), ^{
        if (hud) {
            [self showHUD];
        }
        
      [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    });
    
   
  
    NSDictionary *params = [param mj_keyValues];
    
    [SGHttpTool get:url params:params success:^(id responseObj) {
        if (success && [responseObj isKindOfClass:[NSDictionary class]]) {
          
            if ([[NSString stringWithFormat:@"%@",responseObj[@"state"]] intValue]%100 ==0) {
                  id result = [resultClass mj_objectWithKeyValues:responseObj[@"user_info"]];
               success(result,responseObj);//请求成功
                
            }else//请求失败
            {
           
                if (show && responseObj[@"remark"]/*如果需要显示失败信息，就显示*/) {
                    [SGShowMesssageTool showMessage:[NSString stringWithFormat:@"%@：%@",responseObj[@"remark"],responseObj[@"state"]]];
                }
                if (failure) {
                    failure(nil,responseObj[@"state"],responseObj[@"remark"]);
                }
                
                
            }
            
        }
       
        // 隐藏状态栏上面的菊花
    
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [self hideHUD];
        });
    } failure:^(NSError *error) {
        if (failure) {
            failure(error,[NSString stringWithFormat:@"%ld",error.code],[error localizedDescription]);
        }
        // 隐藏状态栏上面的菊花
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [self hideHUD];
        });
    }];
}




/**
 *   POST请求
 *
 *  @param url          请求地址
 *  @param param       请求参数模型
 *  @param resultClass 请求结果的模型
 *  @param show        请求结果的模型
 *  @param view        将蒙层展示在View上（如果View为空，则不显示蒙层）
 *  @param success     成功的回调
 *  @param failure     失败的回调
 */
+ (void)postWithUrl:(NSString *)url param:(id)param resultClass:(Class)resultClass showResultMessage:(BOOL)show showHUD:(BOOL)hud success:(void (^)(id resultModel,id responseObj))success failure:(void (^)(NSError *error,NSString *errorCode,NSString *remark))failure
{
    // 显示状态栏上面的菊花
    
 
    
    // 没有网络就不去请求数据 做一个友好的提示
    if (![SGShowMesssageTool isHaveNetWork]) {
        if (failure) {
            failure(nil,@"-10086",SGNotNetWorkingTips);
        }
        [SGShowMesssageTool showMessage:SGNotNetWorkingTips];
      
        return;
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
        if (hud) {
            [self showHUD];
        }
        
    });
   
    NSDictionary *params = [param mj_keyValues];
    [SGHttpTool post:url params:params success:^(id responseObj) {
        if (success && [responseObj isKindOfClass:[NSDictionary class]]) {
            if ([[NSString stringWithFormat:@"%@",responseObj[@"state"]] intValue]%100 ==0) {
                id result = [resultClass mj_objectWithKeyValues:responseObj[@"user_info"]];
                success(result,responseObj);//请求成功
            }else//请求失败
            {
                if (show && responseObj[@"remark"]/*如果需要显示失败信息，就显示*/) {
                    [SGShowMesssageTool showMessage:[NSString stringWithFormat:@"%@：%@",responseObj[@"remark"],responseObj[@"state"]]];
                }
                if (failure) {
                    failure(nil,responseObj[@"state"],responseObj[@"remark"]);
                }
                
                
            }
            
        }
        // 隐藏状态栏上面的菊花
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [self hideHUD];
        });
    } failure:^(NSError *error) {
        if (failure) {
            failure(error,[NSString stringWithFormat:@"%ld",error.code],[error localizedDescription]);
        }
        // 隐藏状态栏上面的菊花
        dispatch_async(dispatch_get_main_queue(), ^{
            [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
            [self hideHUD];
        });
    }];
}

#pragma mark --以下两个是内部方法
+ (void)showHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [SGShowMesssageTool showLoadingHUD];
    });
    
    
}

+ (void)hideHUD
{
    dispatch_async(dispatch_get_main_queue(), ^{
       [SGShowMesssageTool hideLoadingHUD];
    });
   
}



@end
